package com.itheima.controller;

import com.itheima.anno.Flag;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理
 */
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门
     */
    @GetMapping
    public Result list() {
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    /**
     * 删除部门
     */
    @DeleteMapping
    @Flag
    public Result delete(Integer id) {
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 添加部门
     */
    @PostMapping
    @Flag
    public Result add(@RequestBody Dept dept) {
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        Dept dept = deptService.get(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     */
    @PutMapping
    @Flag
    public Result update(@RequestBody Dept dept) {
        deptService.update(dept);
        return Result.success();
    }
}